﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;

namespace ProjectWebNhac.GUI.User
{
    public partial class BXH : System.Web.UI.Page
    {
        TheLoaiBus tlbus = new TheLoaiBus();
        int maBXH = 0;
        BXHBus bxhbus = new BXHBus();
        CaSiBus csbus = new CaSiBus();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["MaBXH"] != null)
            {
                maBXH = Convert.ToInt16(Request["MaBXH"]);

            }
            if (!Page.IsPostBack)
            {
                LoadDanhSachBaiHat();
                if (Session["ThanhVien"] != null)
                {
                    NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                    txtUserName.Text = objUser.TaiKhoan;
                }
                loadPlaylist();
            }
        }

        private void LoadDanhSachBaiHat()
        {
            reBaiHat.DataSource = bxhbus.loadBXH(maBXH);
            reBaiHat.DataBind();
        }

        private void loadPlaylist()
        {
            if (Session["ThanhVien"] != null)
            {
                NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                PlaylistBus plBus = new PlaylistBus();
                dropPlaylist.DataSource = plBus.LayPlaylistTheoTaiKhoan(objUser.TaiKhoan);
                dropPlaylist.DataTextField = "TenPlaylist";
                dropPlaylist.DataValueField = "MaPlaylist";
                dropPlaylist.DataBind();
            }
        }


        protected void reBaiHat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            int MaBaiHat;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaBaiHat = Convert.ToInt16(usrCurrent["MaBaiHat"]);
                Repeater reDSCaSiBaiHat = (Repeater)e.Item.FindControl("reDSCaSiBaiHat");
                reDSCaSiBaiHat.DataSource = csbus.LayCaSiTheoBaiHatTable(MaBaiHat);
                reDSCaSiBaiHat.DataBind();
            }
        }

        protected void lnkNgheHet_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChiTietAlbum.aspx?MaBXH=" + maBXH);
        }
        protected void reBaiHat_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "download")
            {
                string linkbai = e.CommandArgument.ToString();
                Response.ContentType = "audio/mp3";
                Response.AddHeader("content-disposition", "attachment; filename=" + linkbai);
                Response.TransmitFile(Server.MapPath("~/Nhac/" + linkbai));
                Response.End();
            }
        }

    }
}